package lch.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import lch.constant.MessageConstant;
import lch.entity.Result;
import lch.pojo.Menu;
import lch.service.ActiveMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lch
 * @date 2020-01-19 17:51
 */
@RequestMapping("/active")
@RestController
public class ActiveMainController {

    @Reference
    private ActiveMainService activeMainService;

    @RequestMapping("/menu")
    public Result menu() {
        //获取用户名
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user == null) {
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
        String username = user.getUsername();
        //获取页面展示菜单数据
        try {
            List<Menu> menuList = activeMainService.menu(username);
            return new Result(true, "菜单展示成功", menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "菜单展示失败");
        }
    }
}